<?php

//购物车控制器
class ShopController extends Controller{
	public function index(){
		if(IS_AJAX){
			$goods=Q('post.');
			$goods['options']=rtrim($goods['options'],',');
			$goods['options']=explode(',', $goods['options']);
			$img=K('Detail')->where("gid={$goods['gid']}")->field('medium')->find();
			$spec=M()->join("__attr__ a JOIN __goods_attr__ ga ON a.taid=ga.taid")->where("gtid in(". implode(',',$goods['options']) .")")->getField('taname',true);
			
			$data=array(
			   'id' =>$goods['gid'],
			   'name' =>$goods['name'],
			   'num' =>$goods['shopnum'],
			   'price' =>$goods['price'],
			   'options' =>$goods['options'],
			   'img' =>$img,
			   'spec'=>$spec,
			   'attrSpec'=>'',
			);
			cart::add($data);
			$qian=array(
			   'numb' => $_SESSION['cart']['total_rows'],//购物车商品数量
			   'total' => $_SESSION['cart']['total']//购物车商品总价
			);
            $this->ajax($qian);
		}
    	$shop=Cart::getGoods();
		$zong=Cart::getAllData();
		$this->assign('zong',$zong);
		
		if($shop){
			$str=array();
		    foreach ($shop as $k => $v) {
		    	foreach ($v['options'] as $kk => $vv) {
		    		   $kk=M()->join("__attr__ a JOIN __goods_attr__ ga ON a.taid=ga.taid")->where("gtid={$vv}")->getField('taname',true);
					   $kk=implode($kk);
					   $da=K('Goodsattr')->where("gtid={$vv}")->getField('gtvalue',true);
					   $shop[$k]['attrSpec'][$kk]=$da;
		    	}
		    }
			$this->assign('shop',$shop);
			
		}
		
		$this->display();
	}

    
	//异步删除购物车里的商品
	public function delshop(){
		if(IS_AJAX){
			$sid=Q('post.sid');
			$data=Q('post.checkBox');
			p($data);
			if(is_string($sid)){
				Cart::del($sid);
				$this->ajax(Cart::getTotalPrice());
			}
			if(is_array($data)){
				foreach ($data as $k => $v) {
				    Cart::del($v);
				}
			}
			
			
		}
        $this->success('删除成功!');
	}

    public function chaxun(){
    	if(IS_AJAX){
           $status = isset($_SESSION['uid'])?1:0;
           $this->ajax($status);
    	}
    	
    }

}








?>